package cn.itcast.shop.user.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.itcast.shop.user.vo.User;

/**
 * 用户模块持久层代码
 * @author zzc
 *
 */
public class UserDao extends HibernateDaoSupport {
   
	/**
	 * 根据用户名判断用户是否存在
	 * @param username
	 * @return
	 */
	public User findByUsername(String username){
		String hql="from User where username=?";
		@SuppressWarnings("unchecked")
		List<User> list= this.getHibernateTemplate().find(hql,username);
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 注册用户存入数据库代码实现
	 */
	public void save(User entity){
		this.getHibernateTemplate().save(entity);
	}

	public User findByCode(String code) {
		String hql="from User where code=?";
		@SuppressWarnings("unchecked")
		List<User> list= this.getHibernateTemplate().find(hql,code);
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	public void update(User existUser) {
		this.getHibernateTemplate().update(existUser);
	}

	public User login(User user) {
		String hql="from User where username=? and password=? and state=1";
		@SuppressWarnings("unchecked")
		List<User> list= this.getHibernateTemplate().find(hql,user.getUsername(),user.getPassword());
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	
	
	
	
	
	
}